package code.assets
{
	import code.interfaces.ISubMenu;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	
	import mx.containers.Grid;
	import mx.containers.GridItem;
	import mx.containers.GridRow;
	import mx.controls.Button;
	import mx.core.BitmapAsset;
	import mx.core.ByteArrayAsset;
	
	public class MenuSurface extends MovieClip implements ISubMenu
	{
		private var id:String;
		private var tWidth:uint;
		private var tHeight:uint;
		private var btn:CloseBtn;
		private var links:XMLList;
		//private var movie:MovieClip;
		private var embeddedSWFLoader:Loader;
		
		[Embed("assets/testTexture.png")]
		public static var Img:Class;
		
		[Embed("assets/menu_anim.swf", mimeType="application/octet-stream")]
		public static var SWF:Class;
		
		public function MenuSurface(links:XMLList)
		{
			super();
			
			this.links = links;
			this.mouseEnabled = false;
		}
		
		protected function addCloseBtn():void
		{
			btn = new CloseBtn();
			btn.linkID = id;
			btn.x = tWidth - btn.width;
			
			this.addChild(btn);
		}
		
		protected function addMovie():void
		{	
			var movie:ByteArrayAsset = new SWF() as ByteArrayAsset;
			embeddedSWFLoader = new Loader();
			var context:LoaderContext = new LoaderContext();
			context.applicationDomain=ApplicationDomain.currentDomain;
			embeddedSWFLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
			
			embeddedSWFLoader.loadBytes(movie, context);
			
		}
		
		protected function onLoaded(evt:Event):void
		{
			var movie:MovieClip = embeddedSWFLoader.content as MovieClip;
			//addChild(movie);
			loadMenu();
		}
		
		public function createTexture():void
		{
			var bm:BitmapAsset = new Img();
			var matrix:Matrix = new Matrix();
			matrix.a = tWidth / bm.width;
			matrix.d = tHeight / bm.height;
			this.graphics.beginBitmapFill(bm.bitmapData, matrix);
			this.graphics.drawRect(0, 0, tWidth, tHeight);
			this.graphics.endFill();
			
			addCloseBtn();
			addMovie();
		}
		
		public function set linkID(nm:String):void
		{
			id = nm;
			
			if(btn)
			{
				btn.linkID = nm;
			}
		}
		
		public function get linkID():String
		{
			return id;
		}
		
		public function loadMenu():void
		{
			/*var hCount:uint = 0;
			var vCount:uint = 0;
			var label:TextField;
			var iPanel:InfoPanel;
			var spacing:uint = tWidth / links.link.length();
			var tf1:TextFormat = new TextFormat();
			var tf2:TextFormat = new TextFormat();
			
			tf1.size = 60;
			tf1.font = "Arial";
			
			tf2.size = 40;
			tf2.font = "Arial";
			

			for each(var link:XML in links.link)
			{
				label = new TextField();
				label.defaultTextFormat = tf1;
				label.mouseEnabled = false;
				label.width = spacing;
				label.height = 200;
				label.wordWrap = true;
				label.text = link.@name;
				label.x = spacing * hCount;
				addChild(label);
				
				for each(var subLink:XML in link.link)
				{
					iPanel = new InfoPanel();
					iPanel.defaultTextFormat = tf2;
					iPanel.width = spacing;
					iPanel.height = 150;
					iPanel.wordWrap = true;
					iPanel.linkID = subLink.@id;
					iPanel.text = subLink.@name;
					iPanel.x = spacing * hCount;
					iPanel.y = 230 + (vCount * 100);
					
					addChild(iPanel);
					
					vCount++;
				}
				vCount = 0;
				hCount++;
				
			}*/
			
		}
		
		public function animate(evt:Event = null):void
		{
			if(embeddedSWFLoader.content)
			{
				((embeddedSWFLoader.content) as MovieClip).play();
				this.removeEventListener(Event.ENTER_FRAME, animate);
			}else
			{
				if(!this.hasEventListener(Event.ENTER_FRAME))
				{
					this.addEventListener(Event.ENTER_FRAME, animate);
				}
			}
		}
		
		override public function set width(val:Number):void
		{
			tWidth = val;
		}
		
		override public function set height(val:Number):void
		{	
			tHeight = val;
		}
	}
}